package com.txhy.certificatechain.presenter.user;

import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.txhy.certificatechain.R;
import com.txhy.certificatechain.api.UrlAddress;
import com.txhy.certificatechain.presenter.base.BasePresenter;
import com.txhy.certificatechain.ui.view.user.RegistView;
import com.txhy.certificatechain.utils.GsonUtil;
import com.txhy.certificatechain.utils.LogUtil;
import com.txhy.certificatechain.utils.UIUtils;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: RegisterPresenter.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 用户注册
 * 创建时间: 2018/9/20 17:32
 * 更新时间：2018/9/20 17:32
 * -----------------------------------------------------------------
 * 2018/9/20 17:32: 创建 RegisterPresenter.java (侯建军);
 * -----------------------------------------------------------------
 */
public class RegisterPresenter extends BasePresenter<RegistView> {
    //注册
    public void toRegist(String phone, String meid, String code) {
        getView().showProgress(UIUtils.getString(R.string.zhengzai));
        OkGo.<String>post(UrlAddress.REGISTER_URL)//
                .tag(this)//
                .params("phone", phone)
                .params("meid", meid)
                .params("phoneModel", "Android")
                .params("verifiCode", code)

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            LogUtil.e("555",response.body());
                            getView().registerSuccess(GsonUtil.GsonGetData(response.body()));
                        } else {
                            getView().registerFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                        if (!TextUtils.isEmpty(response.body())) {
                            getView().registerFail(GsonUtil.GsonGetMsg(response.body()));
                        } else {
                            getView().registerFail("请求失败请检查网络");
                        }
                        getView().hideProgress();
                    }
                });
    }

    //发送短信
    public void SendCode(String phone,String imei) {
        getView().showProgress(UIUtils.getString(R.string.sendcodeok));
        OkGo.<String>get(UrlAddress.SENDCODE_URL)//
                .tag(this)//
                .params("phone", phone)
                .params("meid", imei)
                .execute(new StringCallback() {
                    //发送成功
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().sendCodeSuccess(GsonUtil.GsonGetMsg(response.body()));
                        } else {
                            getView().sendCodeFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }
                    //发送失败
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (!TextUtils.isEmpty(response.body())) {
                            getView().sendCodeFail(GsonUtil.GsonGetMsg(response.body()));
                        } else {
                            getView().sendCodeFail("请求失败请重新发送");
                        }
                        getView().hideProgress();
                    }
                });
    }
}

